package cn.lx.es.query.registrar;

import cn.lx.es.query.annotation.EnableDynamicEsQuery;
import cn.lx.es.query.scanner.ESEntityScanner;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.domain.EntityScanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Set;

/**
 * @author wcy
 * @version 1.0
 * @description: ES自动注册器
 * @date 2025-07-02 上午11:28
 */
public class DynamicEsEntityRegistrar implements ImportBeanDefinitionRegistrar, ApplicationContextAware {



    private ApplicationContext applicationContext;




    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        Map<String, Object> attrs = importingClassMetadata.getAnnotationAttributes(EnableDynamicEsQuery.class.getName());
        String[] basePackages = (String[]) attrs.get("entityBasePackages");

        // 1. 扫描指定包下所有带@EsIndex注解的类

        ESEntityScanner scanner = new ESEntityScanner( registry);
        scanner.setResourceLoader( this.applicationContext);
        scanner.scan(basePackages); // 需实现


    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
